﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoOrionEven : DaoBase
    {
        public DaoOrionEven()
            : base()
        {
            this.TableName = "OrionEven";
            this.ViewName = "VOrionEven";
        }

        public OrionEven SelectById(Int32 OrionId)
        {
            lParam.Clear();
            OrionEven retour = new OrionEven();
            String req = this.DebutSelect
                + this.TableName
                + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@OrionId", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public string GetQuoi(String FormName, String ChampName, String TypeEven)
        {
            lParam.Clear();
            String retour = string.Empty;
            String req = "SELECT quoi FROM orioneven " +
                "LEFT JOIN oriontable ON orioneven.typeevenid = oriontable.orionid and oriontable.orionetat = 1 " +
                "WHERE orioneven.orionetat=1 " +
                "AND FormName=@FormName " +
                "AND ChampName=@ChampName " + 
                "AND Text1=@Text1";

            lParam.Add(new MySqlParameter("@FormName", FormName));
            lParam.Add(new MySqlParameter("@ChampName", ChampName));
            lParam.Add(new MySqlParameter("@Text1", TypeEven));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = dr.GetString(0);
            dr.Close();

            return retour;
        }

        public string GetQuoi(String FormName, String TypeEven)
        {
            lParam.Clear();
            String retour = string.Empty;
            String req = "SELECT quoi FROM orioneven " +
                "LEFT JOIN oriontable ON orioneven.typeevenid = oriontable.orionid and oriontable.orionetat = 1 " +
                "WHERE orioneven.orionetat=1 " +
                "AND FormName=@FormName " +
                "AND Text1=@Text1";

            lParam.Add(new MySqlParameter("@FormName", FormName));
            lParam.Add(new MySqlParameter("@Text1", TypeEven));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = dr.GetString(0);
            dr.Close();

            return retour;
        }

        public List<OrionEven> SelectAllActif()
        {
            lParam.Clear();
            List<OrionEven> retour = new List<OrionEven>();
            String req = this.DebutSelect
                + this.TableName
                 + " WHERE OrionEtat=1";

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        public List<OrionEven> SelectAll()
        {
            lParam.Clear();
            List<OrionEven> retour = new List<OrionEven>();
            String req = this.DebutSelect
                + this.TableName;

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        public OrionEven Insert(OrionEven oe)
        {
            lParam.Clear();
            oe.OrionCrea = DateTime.Now;
            oe.OrionMaj = DateTime.Now;

            OrionEven retour = new OrionEven();
            String req = "INSERT INTO " + TableName + "(OrionId, FormName, ChampName, TypeEvenId, Quoi, OrionCrea, OrionMaj, OrionUserId, OrionEtat)  VALUES(NULL," +
                " @FormName, @ChampName, @TypeEvenId, @Quoi, @OrionCrea, @OrionMaj, @OrionUserId, @OrionEtat)";


            lParam.Add(new MySqlParameter("@FormName", oe.FormName));
            lParam.Add(new MySqlParameter("@ChampName", oe.ChampName));
            lParam.Add(new MySqlParameter("@TypeEvenId", oe.TypeEvenId));
            lParam.Add(new MySqlParameter("@Quoi", oe.Quoi));
            lParam.Add(new MySqlParameter("@OrionCrea", oe.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionEtat", oe.OrionEtat));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }

        public OrionEven Update(OrionEven oe)
        {
            lParam.Clear();
            oe.OrionMaj = DateTime.Now;

            OrionEven retour = new OrionEven();
            String req = "UPDATE " + TableName + " SET FormName=@FormName, ChampName=@ChampName, TypeEvenId=@TypeEvenId, Quoi=@Quoi, OrionMaj=@OrionMaj, OrionUserId=@OrionUserId " +
                " WHERE OrionId=@OrionId";

            lParam.Add(new MySqlParameter("@OrionId", oe.OrionId));
            lParam.Add(new MySqlParameter("@FormName", oe.FormName));
            lParam.Add(new MySqlParameter("@ChampName", oe.ChampName));
            lParam.Add(new MySqlParameter("@TypeEvenId", oe.TypeEvenId));
            lParam.Add(new MySqlParameter("@Quoi", oe.Quoi));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionEtat", oe.OrionEtat));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(oe.OrionId);

            return retour;
        }

        private OrionEven ChargeFromReader(MySqlDataReader dr)
        {
            OrionEven retour = new OrionEven();
            retour.OrionId = dr.GetInt32("OrionId");

            if (dr.IsDBNull(1))
                retour.FormName = string.Empty;
            else
                retour.FormName = dr.GetString("FormName");
            if (dr.IsDBNull(2))
                retour.ChampName = string.Empty;
            else
                retour.ChampName = dr.GetString("ChampName");
            if (dr.IsDBNull(3))
                retour.TypeEvenId = 0;
            else
                retour.TypeEvenId = dr.GetInt32("TypeEvenId");
            if (dr.IsDBNull(4))
                retour.Quoi = string.Empty;
            else
                retour.Quoi = dr.GetString("Quoi");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }
    }
}
